package com.coding;

import com.coding.component.MyLineBorder;
import com.coding.http.ApiService;
import com.coding.util.ToolUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ResetPasswordDialog extends JDialog {
    private static final long serialVersionUID = 1L;

    //自定义圆角输入框边界线
    private static MyLineBorder myLineBorder = new MyLineBorder(new Color(192, 192, 192), 1, true);

    private JTextField account;
    private JPasswordField oldPassword;
    private JPasswordField newPassword;
    private JButton okButton;
    private JButton cancelButton;

    private JLabel msgLabel;

    private String rsString = "";

    private ResetPasswordDialog() {
        super();
        init();
        setModal(true);
        setSize(300, 235);
    }

    private void init() {
        Container contentPane = getContentPane();
        contentPane.setLayout(new FlowLayout());

        JPanel accountPanel = new JPanel();
        accountPanel.add(new JLabel("账  号"));
        account = new JTextField();
        account.setFocusable(true);
        account.setBorder(myLineBorder);
        account.setPreferredSize(new Dimension(150, 25));
        accountPanel.add(account);
        contentPane.add(accountPanel);

        JPanel oldPasswordPanel = new JPanel();
        oldPasswordPanel.add(new JLabel("密  码"));
        oldPassword = new JPasswordField();
        oldPassword.setFocusable(true);
        oldPassword.setBorder(myLineBorder);
        oldPassword.setPreferredSize(new Dimension(150, 25));
        oldPasswordPanel.add(oldPassword);
        contentPane.add(oldPasswordPanel);

        JPanel newPasswordPanel = new JPanel();
        newPasswordPanel.add(new JLabel("新密码"));
        newPassword = new JPasswordField();
        newPassword.setFocusable(true);
        newPassword.setBorder(myLineBorder);
        newPassword.setPreferredSize(new Dimension(150, 25));
        newPasswordPanel.add(newPassword);
        contentPane.add(newPasswordPanel);

        msgLabel = new JLabel("", JLabel.CENTER);
        msgLabel.setPreferredSize(new Dimension(250, 25));
        contentPane.add(msgLabel);

        JPanel panel = new JPanel();
        FlowLayout flowLayout = new FlowLayout();
        flowLayout.setHgap(15);
        panel.setLayout(flowLayout);
        okButton = new JButton("确定");
        okButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                if ("".equals(account.getText()) || "".equals(oldPassword.getText())
                        || "".equals(newPassword.getText())) {
                    ToolUtil.remindMsg("请输入账号和密码");
                    return;
                }

                try {
					String msg = ApiService.updPassWord(account.getText(), oldPassword.getText(),
							newPassword.getText());

					msgLabel.setText(msg);
				} catch (Exception ex){
					msgLabel.setText("请求异常,联系管理员");
				}
                /*if ("修改成功".equals(msg)) {
                    rsString = account.getText();
                    dispose();
                }*/
            }
        });

        cancelButton = new JButton("取消");
        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
        panel.add(okButton);
        panel.add(cancelButton);

        contentPane.add(panel, BorderLayout.SOUTH);
    }

    public static String showDialog(Component relativeTo, String text) {
        ResetPasswordDialog dialog = new ResetPasswordDialog();
        dialog.account.setText(text);
        dialog.rsString = text;
        dialog.setLocationRelativeTo(relativeTo);
        dialog.setVisible(true);
        return dialog.rsString;
    }

}
